﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.IO;
using System.Collections.Generic;
using System.Globalization;

namespace CarBloke
{
    public class ApplicationSettings
    {
        public enum VolumeUnits
        {
            Liter,
            Gallon,
            ImperialGallon
        }

        public enum DistanceUnits
        {
            Kilometers,
            Miles
        }

        public enum EfficiencyUnits
        {
            MilesPerGallon,
            MilesPerImperialGallon,
            KilometersPerLiter,
            LiterPer100Kilometers
        }

        private static ApplicationSettings settings;

        private IsolatedStorageSettings isolatedStorageSettings = IsolatedStorageSettings.ApplicationSettings;

        private ApplicationSettings()
        {
        }

        public static ApplicationSettings Current
        {
            get
            {
                if (settings == null)
                {
                    settings = new ApplicationSettings();
                }

                return settings;
            }
        }

        public EfficiencyUnits EfficiencyUnit
        {
            get
            {
                EfficiencyUnits currentEfficiencyUnit = defaultEfficiencyUnit();
                if (isolatedStorageSettings.Contains("EfficiencyUnit"))
                {
                    currentEfficiencyUnit = (EfficiencyUnits)isolatedStorageSettings["EfficiencyUnit"];
                }
                else
                {
                    EfficiencyUnit = currentEfficiencyUnit;
                }

                return currentEfficiencyUnit;
            }
            set
            {
                if (isolatedStorageSettings.Contains("EfficiencyUnit"))
                {
                    isolatedStorageSettings["EfficiencyUnit"] = value;
                }
                else
                {
                    isolatedStorageSettings.Add("EfficiencyUnit", value);
                }

                isolatedStorageSettings.Save();
            }
        }


        private EfficiencyUnits defaultEfficiencyUnit()
        {
            EfficiencyUnits defaultUnit = EfficiencyUnits.LiterPer100Kilometers;
            String currentCulture = CultureInfo.CurrentCulture.Name;

            switch (currentCulture)
            {
                case "en-US":
                    defaultUnit = EfficiencyUnits.MilesPerGallon;
                    break;
                case "en-GB":
                    defaultUnit = EfficiencyUnits.MilesPerImperialGallon;
                    break;
            }

            return defaultUnit;
        }

        public VolumeUnits VolumeUnit
        {
            get
            {
                VolumeUnits currentVolumeUnit = defaultVolumeUnit();
                if (isolatedStorageSettings.Contains("VolumeUnit"))
                {
                    currentVolumeUnit = (VolumeUnits)isolatedStorageSettings["VolumeUnit"];
                }
                else
                {
                    VolumeUnit = currentVolumeUnit;
                }

                return currentVolumeUnit;
            }
            set
            {
                if (isolatedStorageSettings.Contains("VolumeUnit"))
                {
                    isolatedStorageSettings["VolumeUnit"] = value;
                }
                else
                {
                    isolatedStorageSettings.Add("VolumeUnit", value);
                }

                isolatedStorageSettings.Save();
            }
        }

        private VolumeUnits defaultVolumeUnit()
        {
            VolumeUnits defaultUnit = VolumeUnits.Liter;
            String currentCulture = CultureInfo.CurrentCulture.Name;

            switch (currentCulture)
            {
                case "en-US":
                    defaultUnit = VolumeUnits.Gallon;
                    break;
                case "en-GB":
                    defaultUnit = VolumeUnits.ImperialGallon;
                    break;
            }

            return defaultUnit;
        }

        public DistanceUnits DistanceUnit
        {
            get
            {
                DistanceUnits currentDistanceUnit = defaultDistanceUnit();
                if (isolatedStorageSettings.Contains("DistanceUnit"))
                {
                    currentDistanceUnit = (DistanceUnits)isolatedStorageSettings["DistanceUnit"];
                }
                else
                {
                    DistanceUnit = currentDistanceUnit;
                }

                return currentDistanceUnit;
            }
            set
            {
                if (isolatedStorageSettings.Contains("DistanceUnit"))
                {
                    isolatedStorageSettings["DistanceUnit"] = value;
                }
                else
                {
                    isolatedStorageSettings.Add("DistanceUnit", value);
                }

                isolatedStorageSettings.Save();
            }
        }

        private DistanceUnits defaultDistanceUnit()
        {
            DistanceUnits defaultUnit = DistanceUnits.Kilometers;
            String currentCulture = CultureInfo.CurrentCulture.Name;

            switch (currentCulture)
            {
                case "en-US":
                case "en-GB":
                    defaultUnit = DistanceUnits.Miles;
                    break;
            }

            return defaultUnit;
        }

        public static String ToLongString(ApplicationSettings.VolumeUnits units, Boolean plural)
        {
            String result = String.Empty;

            switch (units)
            {
                case ApplicationSettings.VolumeUnits.Gallon:
                    result = plural ? LanguageResources.Resources._gallonsUS : LanguageResources.Resources._gallonUS;
                    break;
                case ApplicationSettings.VolumeUnits.ImperialGallon:
                    result = plural ? LanguageResources.Resources._gallonsUK : LanguageResources.Resources._gallonUK;
                    break;
                case ApplicationSettings.VolumeUnits.Liter:
                    result = plural ? LanguageResources.Resources._liters : LanguageResources.Resources._liter;
                    break;
            }

            return result;
        }

        public static String ToShortString(ApplicationSettings.VolumeUnits units)
        {
            String result = String.Empty;

            switch (units)
            {
                case ApplicationSettings.VolumeUnits.Gallon:
                    result = LanguageResources.Resources._gallonShortUS;
                    break;
                case ApplicationSettings.VolumeUnits.ImperialGallon:
                    result = LanguageResources.Resources._gallonShortUK;
                    break;
                case ApplicationSettings.VolumeUnits.Liter:
                    result = LanguageResources.Resources._literShort;
                    break;
            }

            return result;
        }

        public static String ToLongString(ApplicationSettings.DistanceUnits units, Boolean plural)
        {
            String result = String.Empty;

            switch (units)
            {
                case ApplicationSettings.DistanceUnits.Miles:
                    result = plural ? LanguageResources.Resources._miles : LanguageResources.Resources._mile;
                    break;
                case ApplicationSettings.DistanceUnits.Kilometers:
                    result = plural ? LanguageResources.Resources._kilometers : LanguageResources.Resources._kilometer;
                    break;              
            }

            return result;
        }

        public static String ToShortString(ApplicationSettings.DistanceUnits units)
        {
            String result = String.Empty;

            switch (units)
            {
                case ApplicationSettings.DistanceUnits.Miles:
                    result = LanguageResources.Resources._mileShort;
                    break;
                case ApplicationSettings.DistanceUnits.Kilometers:
                    result = LanguageResources.Resources._kilometerShort;
                    break;
            }

            return result;
        }

        public static String ToString(ApplicationSettings.EfficiencyUnits units)
        {
            String result = String.Empty;

            switch (units)
            {
                case ApplicationSettings.EfficiencyUnits.MilesPerGallon:
                    result = LanguageResources.Resources._mpgUS;
                    break;
                case ApplicationSettings.EfficiencyUnits.MilesPerImperialGallon:
                    result = LanguageResources.Resources._mpgUK;
                    break;
                case ApplicationSettings.EfficiencyUnits.LiterPer100Kilometers:
                    result = LanguageResources.Resources._lPer100Km;
                    break;
                case ApplicationSettings.EfficiencyUnits.KilometersPerLiter:
                    result = LanguageResources.Resources._kmPerL;
                    break;
            }

            return result;
        }

    }
}
